package xin.lhuo.we.server.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import xin.lhuo.we.server.entity.Album;
import xin.lhuo.we.server.entity.User;
import xin.lhuo.we.server.enums.ResultEnum;
import xin.lhuo.we.server.service.IAlbumService;
import xin.lhuo.we.server.utils.ResultVoUtil;
import xin.lhuo.we.server.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static xin.lhuo.we.server.constant.HttpConstant.SESSION_USER;

/**
 * @author Lhuo
 * @date 2019/5/11 19:32
 */
@Slf4j
@RestController
@RequestMapping(value = "/album")
public class AlbumController {
    private final IAlbumService albumServer;

    public AlbumController(IAlbumService albumServer) {
        this.albumServer = albumServer;
    }

    @PostMapping("/create")
    public ResultVo createAlbum(HttpServletRequest request,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "description", required = false) String description) {
        if (StringUtils.isEmpty(name)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "name不能为空");
        }
        User user = getUser(request);
        Album album = new Album();
        album.setName(name);
        album.setUserId(user.getId());
        album.setDescription(description);
        albumServer.createAlbum(album,user.getId());
        return ResultVoUtil.success();
    }

    @GetMapping("/info")
    public ResultVo getAlbum(HttpServletRequest request) {
        User user = getUser(request);
        return ResultVoUtil.success(albumServer.getAlbums(user.getId()));
    }

    @DeleteMapping()
    public ResultVo deleteAlbum(@RequestParam("id") Integer id, HttpServletRequest request) {
        albumServer.deleteAlbum(id, getUser(request).getId());
        return ResultVoUtil.success();
    }

    @PostMapping(value = "/rename")
    public ResultVo renameAlbum(Integer id, String name, HttpServletRequest request) {
        if (id == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "id不能为空");
        }
        if (StringUtils.isEmpty(name)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "name不能为空");
        }
        albumServer.rename(id, name, getUser(request).getId());
        return ResultVoUtil.success();
    }

    @GetMapping(value = "/upload_token")
    public ResultVo getUploadToken() {
        return ResultVoUtil.success(albumServer.getUploadAlbumToken());
    }

    @PutMapping(value = "/cover_url")
    public ResultVo setAlbumCoverUrl(Integer id, String key, HttpServletRequest request) {
        if (StringUtils.isEmpty(key)) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "key不能为空");
        }
        if (id == null) {
            return ResultVoUtil.error(ResultEnum.PARAM_ERROR.getCode(), "id不能为空");
        }
        albumServer.updateAlbumCoverUrl(id, key, getUser(request).getId());
        return ResultVoUtil.success();
    }

    private User getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute(SESSION_USER);
    }

}
